package com.bltec.emergency.controller.recordController;


import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.RecordResponse;
import com.bltec.emergency.service.recordService.RecordResponseService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/response")
@RequiresPermissions("project:record")
public class ResponseController {
    @Resource
    private RecordResponseService recordResponseService;
    @Resource
    private UserService userService;
    /**
     * 获得所有response
     */
    @RequestMapping("getAllResponse")
    public ResponseJson getAllResponse() {
        try {
            List<RecordResponse> recordResponse = recordResponseService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(recordResponse));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加response
     */
    @RequestMapping("addResponse")
    @RequiresPermissions("record:insert")
    public ResponseJson addResponse(String name, Integer responseLevel, String occurredPosition, String occurredReason, String influenceScope, Date occurredTime, String casualties) {
        try {
            RecordResponse response = new RecordResponse();
            response.setName(name);
            response.setResponseLevel(responseLevel);
            response.setOccurredPosition(occurredPosition);
            response.setOccurredReason(occurredReason);
            response.setInfluenceScope(influenceScope);
            response.setOccurredTime(occurredTime);
            response.setCasualties(casualties);
            response.setEnable(true);
            response.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            response.setCreateTime(DateUtil.dateNow());
            recordResponseService.add(response);
            return new ResponseJson(Constant.OK, "创建exercise成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建exercise失败" + e.getMessage());
        }
    }

    /**
     * 修改exercise
     */
    @RequiresPermissions("record:update")
    @RequestMapping("updateResponse")
    public ResponseJson updateUser(Long id,String name, Integer responseLevel, String occurredPosition, String occurredReason, String influenceScope, Date occurredTime, String casualties) {
        try {
            RecordResponse response = recordResponseService.getResponseById(id);
            response.setName(name);
            response.setResponseLevel(responseLevel);
            response.setOccurredPosition(occurredPosition);
            response.setOccurredReason(occurredReason);
            response.setInfluenceScope(influenceScope);
            response.setOccurredTime(occurredTime);
            response.setCasualties(casualties);
            response.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            response.setUpdateTime(DateUtil.dateNow());
            recordResponseService.updateResponseById(response);
            return new ResponseJson(Constant.OK, JSON.toJSON(response));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除response
     */
    @RequiresPermissions("record:delete")
    @RequestMapping("deleteResponse")
    public ResponseJson setDisable(Long id) {
        if (null != recordResponseService.getResponseById(id)) {
            if (recordResponseService.deleteResponseById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }

}
